import { defineComponent, reactive, ref, toRefs } from 'vue'
import { NForm, NFormItem, NModal, FormRules, NSelect } from 'naive-ui'
import { updateUserRoleReq } from '@/service/modules/user'
import { Response } from '@/service/modules/common/types'
import { renderMultipleSelectTag } from '@/common/common'
import { useRole } from '../common/use-role'

const ModUserRoleModal = defineComponent({
  name: 'ModUserRoleModal',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    userId: {
      type: Number,
      required: true,
      default: 0
    }
  },
  emits: ['modalShowChange', 'refreshList'],
  setup() {
    const state = reactive({
      userInfoForm: {
        userId: 0,
        roleId: '',
        roleIds: []
      },
      positiveButtonDisabled: ref(false),
      rules: {
        roleIds: {
          trigger: ['input', 'blur'],
          required: true,
          validator() {
            if (state.userInfoForm.roleIds.length === 0) {
              return new Error(window.$t('user.roleIdTips'))
            }
          }
        }
      } as FormRules
    })
    //系统角色下拉选项
    const { roleVariables, getRoleOptions } = useRole()
    getRoleOptions(0)
    return {
      ...toRefs(state),
      roleVariables
    }
  },
  methods: {
    async onPositiveClick() {
      //输入框存在为空时则确认按钮无效
      if (this.userInfoForm.roleIds.length === 0 || this.userId === 0) {
        return
      }
      //提交创建用户请求
      this.positiveButtonDisabled = true
      const res: Response<any> = await updateUserRoleReq(
        this.userId,
        this.userInfoForm.roleIds
      ).catch(() => {
        this.positiveButtonDisabled = false
      })
      if (res.resp_code === 0) {
        this.initData()
        this.$emit('modalShowChange', false)
        this.$emit('refreshList')
        this.positiveButtonDisabled = false
      } else {
        this.positiveButtonDisabled = false
      }
    },
    onNegativeClick() {
      this.initData()
      this.$emit('modalShowChange', false)
    },
    initData() {
      this.userInfoForm.userId = 0
      this.userInfoForm.roleId = ''
      this.userInfoForm.roleIds = []
    }
  },
  render() {
    return (
      <NModal
        show={this.showModal}
        mask-closable={false}
        style='width: 400px'
        title={window.$t('user.modRoles')}
        bordered={false}
        size='huge'
        preset='dialog'
        positive-text={window.$t('dialog.positiveText')}
        negative-text={window.$t('dialog.negativeText')}
        onPositiveClick={this.onPositiveClick}
        onNegativeClick={this.onNegativeClick}
        positiveButtonProps={{
          disabled:
            this.positiveButtonDisabled ||
            this.userInfoForm.roleIds.length === 0
        }}
        onClose={this.onNegativeClick}
      >
        <NForm
          rules={this.rules}
          label-placement='left'
          require-mark-placement='right-hanging'
          label-width='auto'
        >
          <NFormItem path='roleIds' label={window.$t('user.roles')}>
            <NSelect
              v-model={[this.userInfoForm.roleIds, 'value']}
              multiple
              options={this.roleVariables.roleOptions}
              render-tag={renderMultipleSelectTag}
            />
          </NFormItem>
        </NForm>
      </NModal>
    )
  }
})

export default ModUserRoleModal
